package slent.iespana.es.visualizacion;

import com.sun.opengl.util.GLUT;
import es.iespana.slent.reaccion.Atomo;
import javax.media.opengl.GL;
import javax.swing.JOptionPane;
import slent.iespana.es.visualizacion.DibujadorMolecula;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author slent
 */
public class MoleculaH2O extends DibujadorMolecula {

    private Atomo atomoO;
    private Atomo atomoH1;
    private Atomo atomoH2;
    boolean isHO = false;
    private int secondsEspera = 0;

    public MoleculaH2O() {
        desplazX = 0.05;
        desplazY = 0.07;
        desplazZ = 0.01;
    }

    public void preparar() {
        atomoH1 = atomoO.getAtomo().get(0);
    }

    public void dibujar(GL gl) {

        if (reaccionando) {
            dibujarReaccion(gl);
        }
        if (!isHO) {//osea si no ha reaccionado
            gl.glPushMatrix();
            //vamos desplazando la molecula por todos lados
            desplazar(gl);

            //atomo central
            gl.glTranslatef(0.0f, 0.0f, 0.1171f);
            color = ColorJOGL.transformarColor(atomoO.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoO.getRadioRelativo())), 20, 20);

            gl.glColor3d(1, 1, 1);
            //H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0.7615f, -0.4790f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
//        System.out.println("Color R:"+color.getRed()+" G:"+color.getGreen()+" B:"+color.getBlue());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //H2
            gl.glPushMatrix();
            gl.glTranslatef(0.0f, -0.7615f, -0.4790f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();


            //enlaces

            //centro -> H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(230, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.7615f, 30, 1);
            gl.glPopMatrix();

            //centro -> H2
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(-230, 1, 0, 0);
//            gl.glRotated(28, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.7615f, 30, 1);
            gl.glPopMatrix();

            gl.glPopMatrix();
        }
        if (isHO) {
            gl.glPushMatrix();
            //vamos desplazando la molecula por todos lados
            desplazar(gl);

            //atomo central
            gl.glTranslatef(0.0f, 0.0f, 0.1171f);
            color = ColorJOGL.transformarColor(atomoO.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoO.getRadioRelativo())), 20, 20);

            gl.glColor3d(1, 1, 1);
            //H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0.7615f, -0.4790f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
//        System.out.println("Color R:"+color.getRed()+" G:"+color.getGreen()+" B:"+color.getBlue());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //enlaces

            //centro -> H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(230, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.7615f, 30, 1);
            gl.glPopMatrix();

            gl.glPopMatrix();
        }
    }

    @Override
    public void dibujarReaccion(GL gl) {
        super.dibujarReaccion(gl);
//        try {
//            Thread.sleep(1000);
//        } catch (Exception exc) {
//            System.out.println("No esperó");
//        }
        secondsEspera += 1;
        if (secondsEspera > 3) {
            secondsEspera = 0;
            reaccionando = false;
            isHO = !isHO;
        }
    }

    public Atomo getAtomoH1() {
        return atomoH1;
    }

    public void setAtomoH1(Atomo atomoH1) {
        this.atomoH1 = atomoH1;
    }

    public Atomo getAtomoH2() {
        return atomoH2;
    }

    public void setAtomoH2(Atomo atomoH2) {
        this.atomoH2 = atomoH2;
    }

    public Atomo getAtomoO() {
        return atomoO;
    }

    public void setAtomoO(Atomo atomoO) {
        this.atomoO = atomoO;
    }
}
